package com.alili.springbooteditor.controller;

import com.alili.springbooteditor.dao.CommentsDao;
import com.alili.springbooteditor.dao.EditorsDao;
import com.alili.springbooteditor.dao.MessagesDao;
import com.alili.springbooteditor.entities.comments;
import com.alili.springbooteditor.entities.editors;
import com.alili.springbooteditor.entities.messages;
import com.alili.springbooteditor.repository.EditorRepository;
import com.alili.springbooteditor.repository.MessagesRepository;
import com.alili.springbooteditor.service.EditorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class mainController {
    @Autowired
    EditorRepository editorRepository;
    @Autowired
    EditorsDao editorsDao;
    @Autowired
    MessagesRepository messagesRepository;
    @Autowired
    MessagesDao messagesDao;
    @Autowired
    EditorService editorService;
    @Autowired
    CommentsDao commentsDao;
    //来到管理文章页面
    @GetMapping("main/manage")
    public String toManagePage(@RequestParam(defaultValue = "0",value = "currentPage") int  pageNum, ModelMap modelMap, Model model){
        Page<editors> p = editorsDao.findAllEditorsAndSortByDate(pageNum);//按照日期排序和分页显示
        List<editors> allAticle = p.getContent();
        model.addAttribute("pages",p);
        modelMap.addAttribute("arts",allAticle);
        return "manage/manage";
    }
    //删除文章
    @DeleteMapping("/manage/{id}")
    public String deleteEditor(@PathVariable("id") Integer id){
        commentsDao.partDeleteCommentsByeditorId(id);
        editorsDao.deleteE(id);
        return "redirect:/main/manage";
    }
    //来到修改文章页面
    @GetMapping("main/alter/{id}")
    public String toAlterEditorPage(@PathVariable("id") Integer id,ModelMap modelMap){
        editors oneArticle = editorsDao.getOneArticle(id);
        modelMap.addAttribute("art",oneArticle);
        return "manage/alterEditor";
    }
    //保存修改的文章
    @PutMapping("/alter")
    public String SaveAlterEditor(editors editor){
        editorsDao.save(editor);
        return "redirect:/main/manage";
    }
    //来到管理留言页面
    @GetMapping("/main/message")
    public String toMessagePage(@RequestParam(defaultValue = "0",value = "currentPage") int  pageNum, ModelMap modelMap, Model model){
        PageRequest pageable = PageRequest.of(pageNum, 3, Sort.Direction.DESC, "messageId");
        Page<messages> p = messagesDao.findAllMessagesAndSortBymessageId(pageNum);
        List<messages> allMessages = p.getContent();
        model.addAttribute("pages",p);
        modelMap.addAttribute("messages",allMessages);
        return "manage/manageMessage";
    }
    //删除留言
    @DeleteMapping("message/{messageId}")
    public String deleteMessages(@PathVariable("messageId") Integer messageId){
        messagesDao.deleteMessage(messageId);
        return "redirect:/main/message";
    }
    //删除评论
    @DeleteMapping("main/detail/{id}")
    public String mainSubmitCommentAndShowDetail(@RequestParam(defaultValue = "0",value = "currentPage") int  pageNum, @PathVariable("id") Integer id, Integer commentsId ,
                                                 ModelMap modelMap, ModelMap modelMap2, ModelMap modelMap3, ModelMap modelMap4, ModelMap modelMap5){
        //展示文章
        editors oneArt = editorsDao.getOneArticle(id);
        modelMap.addAttribute("artdet",oneArt);
        editors preEditor = editorService.getPreEditor(id);
        editors nextEditor = editorService.getNextEditor(id);
        modelMap2.addAttribute("pre",preEditor);
        modelMap3.addAttribute("next",nextEditor);
        //删除评论
        commentsDao.deleteComments(commentsId);
        //展示评论
        Page<comments> page = commentsDao.getCommentsPage(id, pageNum);
        List<comments> allComments=page.getContent();
        modelMap4.addAttribute("pages",page);
        modelMap5.addAttribute("comments",allComments);

        return "manage/detail";
    }
}
